package com.example.myapplication
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentValues.TAG
import android.content.Intent
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.Button
import com.example.total.R
import com.threed.jpct.*
import javax.microedition.khronos.egl.EGL10
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.egl.EGLDisplay

/**

A simple demo. This shows more how to use jPCT-AE than it shows how to write a proper application for Android.

It includes basic activity management to handle pause and resume...

@author EgonOlsen
 */
class ModelActivity : Activity() {

    // Used to handle pause and resume...
    private var render: MyRender? = null//参数设置
    private var fb: FrameBuffer? = null
    private var world: World? = null
    private val back = RGBColor(50, 50, 100)

    private var touchTurn = 0f
    private var touchTurnUp = 0f

    private var xpos = -1f
    private var ypos = -1f

    private var cube: Object3D? = null
    private var fps = 0

    private var sun: Light? = null

    companion object {
        var master: DetailPageActivity? = null
    }

    @SuppressLint("NewApi")
    public override fun onCreate(savedInstanceState: Bundle?) {
        Logger.log("onCreate")

        if (master != null) {
            copy(master!!)
        }

        super.onCreate(savedInstanceState)
        mGLView = GLSurfaceView(getApplication())
        mGLView.setEGLConfigChooser(object : GLSurfaceView.EGLConfigChooser {
            override fun chooseConfig(egl: EGL10, display: EGLDisplay): EGLConfig? {
                // Ensure that we get a 16-bit framebuffer. Otherwise, we'll fall back to Pixelflinger on some device (read: Samsung I7500)
                val attributes = intArrayOf(EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE)
                val configs = arrayOfNulls<EGLConfig>(1)
                val result = IntArray(1)
                if (!egl.eglChooseConfig(display, attributes, configs, 1, result) || result[0] == 0) {
                    // Fall back to default configuration if the requested configuration is not available
                    Log.w(TAG, "Could not find desired EGL configuration, falling back to default.")
                    return null
                }
                return configs[0]
            }
        })


        render = MyRender(this)
        mGLView.setRenderer(render)

        setContentView(mGLView)
    }

    override fun onPause() {
        super.onPause()
        mGLView.onPause()
    }

    override fun onResume() {
        super.onResume()
        mGLView.onResume()
    }

    override fun onStop() {
        super.onStop()
    }

    private fun copy(src: Any) {
        try {
            Logger.log("Copying data from master Activity!")
            val fs = src.javaClass.getDeclaredFields()
            for (f in fs) {
                f.setAccessible(true)
                f.set(this, f.get(src))
            }
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }

    private lateinit var mGLView: GLSurfaceView//模型预览窗口

    override fun onTouchEvent(me: MotionEvent): Boolean {
        if (me.action == MotionEvent.ACTION_DOWN) {
            xpos = me.getX()
            ypos = me.getY()
            return true
        }

        if (me.action == MotionEvent.ACTION_UP) {
            xpos = -1f
            ypos = -1f
            touchTurn = 0f
            touchTurnUp = 0f
            render?.setTouchTurn(0f)
            render?.setTouchTurnUp(0f)
            return true
        }

        if (me.action == MotionEvent.ACTION_MOVE) {
            val xd = me.getX() - xpos
            val yd = me.getY() - ypos

            xpos = me.getX()
            ypos = me.getY()

            touchTurn = xd / -100f
            touchTurnUp = yd / -100f

            render?.setTouchTurn(touchTurn)
            render?.setTouchTurnUp(touchTurnUp)

            // println("Move")
            return true
        }

        try {
            Thread.sleep(15)
        } catch (e: Exception) {
            // No need for this...
        }

        return super.onTouchEvent(me)
    }
}


